import 'package:flutter/material.dart';
import 'dart:async';

class Toast {
  final String tips;
  final BuildContext context;
  final int millis;
  final IconData icon;
  final bool needIcon;

  Toast({
    @required this.context,
    @required this.tips,
    this.millis = 2000,
    this.icon = Icons.error_outline,
    this.needIcon = true
  });


  void _toastBasic(Widget widget) async{
    /// Overlay相当于浮层上下文
    OverlayState overlayState = Overlay.of(context);
    OverlayEntry overlayEntry = OverlayEntry(builder: (BuildContext context) => widget);
    /// 将浮层插入上下文
    overlayState.insert(overlayEntry);
    await Future.delayed(Duration(milliseconds: millis));
    /// 移除浮层
    overlayEntry.remove();
  }

  void showToast() {
     Widget widget = Container(
      width: MediaQuery.of(context).size.width,
      height: MediaQuery.of(context).size.height,
      color: Colors.transparent,
      child: Center(
        child: Container(
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
            color: Color.fromARGB(100, 0, 0, 0),
            borderRadius: BorderRadius.circular(6.0)
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              needIcon ? Icon(icon, color: Colors.white, size: 50.0,) : Row(mainAxisSize: MainAxisSize.min,),
              needIcon ? SizedBox(height: 6.0,) : Row(mainAxisSize: MainAxisSize.min,),
              Text(
                tips,
                style: TextStyle(
                  fontSize: 14.0,
                  fontWeight: FontWeight.w200,
                  color: Colors.white,
                  decoration: TextDecoration.none
                ),
              ),
            ],
          )
        ),
      ),
    );
    _toastBasic(widget);
  }
}